package com.meida.module.bus.provider.controller;

import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.springmvc.base.BaseController;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.BusMaterialPackage100;
import com.meida.module.bus.client.entity.BusMaterialPackage200;
import com.meida.module.bus.client.entity.BusProject;
import com.meida.module.bus.client.entity.BusVersion;
import com.meida.module.bus.provider.service.BusMaterialPackage100Service;
import com.meida.module.bus.provider.service.BusMaterialPackage200Service;
import com.meida.module.bus.provider.service.BusProjectService;
import com.meida.module.bus.provider.service.BusVersionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * 100/200分包控制器
 *
 * @author flyme
 * @date 2020-10-10
 */
@RestController
@RequestMapping("/bus/package/")
public class BusMaterialPackageController extends BaseController<BusMaterialPackage100Service, BusMaterialPackage100> {
    @Autowired
    BusProjectService busProjectService;
    @Autowired
    BusVersionService busVersionService;
    @Autowired
    BusMaterialPackage200Service busMaterialPackage200Service;

    @ApiOperation(value = "100/200分包-分页列表", notes = "100/200分包分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return bizService.getPackagePageList(params);
    }

    @ApiOperation(value = "100/200分包-导出模板", notes = "100/200分包-导出模板")
    @PostMapping(value = "exportTemplate")
    public void exportTemplate(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) Map params) {
        Long projectId = TypeUtils.castToLong(params.get("projectId"));
        BusProject busProject = busProjectService.getById(projectId);
        Long versionId = busProject.getVersionpackageId();
        BusVersion busVersion = busVersionService.getById(versionId);
        Integer bigVersion = FlymeUtils.getInteger(busVersion.getBigVersion(), 0);
        int version = bigVersion / 100;
        if (version == 1) {
            bizService.export(params, request, response, "对内月报模板表", "对内月报填报（必须是同一个包商的填报量）", "busMaterialPackage100ReportExportHandler");
        }
        if (version == 2) {
            busMaterialPackage200Service.export(params, request, response, "对内月报模板表", "对内月报填报（必须是同一个包商的填报量）", "busMaterialPackage200ReportExportHandler");
        }
    }
}
